#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年7月24日

@author: chenyongbing
'''
import sys,os
import logging
import tarfile,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

def openTarFile(rawfile_tar):
    output = ''
    if not os.path.exists(rawfile_tar):
        logging.error('file %s not exists.'%rawfile_tar)
        return output
    #tar = tarfile.open(rawfile_tar)
    try:
        tar = tarfile.open(rawfile_tar)
        
    except :
        logging.error('tar file error.')
        return output
    for member in tar.getmembers():
        f = tar.extractfile(member)
        
        out = f.read()
        output = output + out+'\n'
    tar.close()
    return output




def analyze_rawdata(raw_file):
    if re.search('.tar.gz',raw_file):
        output = openTarFile(raw_file)
    else:
        output = open(raw_file).read()
        
        
    for line in output.split('\n'):
        lineList = line.split('.')
        if len(lineList)!=6:continue
        day = lineList[0]
        device = lineList[1]
        if len( device)>12:continue
        mac = lineList[2]
        stime = lineList[3]
        rssi = lineList[4]
        yield day,device,mac,stime,rssi
        
        
if __name__=='__main__':
    for line in analyze_rawdata('/Users/chenyongbing/Downloads/32010078.txt.tar.gz'):
        print line

